<?php


namespace app\common\model\european;

use app\common\model\BaseModel;

use app\common\model\order\Order;
use app\common\model\order\OrderProduct;
use app\common\model\product\Product;

use app\common\model\supplier\Supplier;

/**
 * 门店订单模型
 */
class EuropeanUserPicking extends BaseModel
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'app_id',
        'update_time',
    ];

    /**
     * 创建数据
     */
    public static function createData($data)
    {
        $data['app_id'] = self::$app_id;
        return self::create($data);
    }

    /**
     * 删除售后订单的竞猜比分记录
     * deleteData
     * void
     * static
     * @return void
     * @Author kevin
     * @date 2024/6/4 14:22
     */
    public static function deleteData($order_no=''){
        if(self::where('order_no',$order_no) -> count()){
            self::where('order_no',$order_no) -> update(['is_delete' => 1]);
        }else{
            Order::where('order_no',$order_no) -> dec('picking_num',1) -> update();
        }

    }

    /**
     *更新参与竞猜商品的相关参数
     * updateOrderData
     * @param $order_id
     * @param $user_id
     * true
     * @return true
     * @throws \think\db\exception\DbException
     * static
     * @throws \think\db\exception\DbException
     * @Author kevin
     * @date 2024/6/6 9:38
     *
     */
    public static function updateOrderData($order_id,$type=1)
    {

        $product_ids = OrderProduct::where('order_id',$order_id) -> column('product_id');
        $map = [];
        $map[] = ['product_id','in',$product_ids];
        $map[] = ['is_picking','=',1];
        if(Product::where($map) -> count()){
            $update_data['is_join'] = 1;
            if($type==1){
                $update_data['picking_num'] = 1;
            }
            Order::where('order_id',$order_id) -> update($update_data);
            return 1;
        }
        return 0;
    }

    /**
     *比分竞猜列表
     * getList
     * void
     * static
     * @return void
     * @Author kevin
     * @date 2024/6/7 12:25
     */
    public static function getList($params){
        $european_score = EuropeanScore::column('score','european_score_id');

        $european_match = EuropeanMatch::column('team','european_match_id');


        $map = [];
        if(isset($params['shop_supplier_id']) && $params['shop_supplier_id']){
            $map[] = ['shop_supplier_id','=',$params['shop_supplier_id']];
        }
        $supplier = Supplier::where('is_delete',0) -> column('name','shop_supplier_id');
        $list = self::where($map) -> paginate($params);
        foreach ($list as $key => $value){
            $list[$key]['score_text'] = $european_score[$value['european_score_id']]??'';
            $list[$key]['result_score_text'] = $european_score[$value['result_european_score_id']]??'待公布';
            $list[$key]['european_match_text'] = $european_match[$value['european_match_id']]??'';
            $list[$key]['shop_supplier_name'] = $supplier[$value['shop_supplier_id']] ??'';
            $list[$key]['status_text'] = '正常';
            if($value['is_delete'] == 1){
                $list[$key]['status_text'] = '已作废（申请售后）';
            }
            if($value['is_prize'] == 1){
                $list[$key]['is_prize_text'] = '已猜中';
            }elseif($value['is_prize'] == 2){
                $list[$key]['is_prize_text'] = '未猜中';
            }else{
                $list[$key]['is_prize_text'] = '待公布';
            }
            $list[$key]['is_give_text'] = '';
            if($value['is_prize'] == 1){
                if($value['is_give'] == 1){
                    $list[$key]['is_give_text'] = '已发放';
                }else{
                    $list[$key]['is_give_text'] = '待发放';
                }
            }

        }
        return $list;
    }

}